package com.xiaoniu.forum.service.impl;

import com.xiaoniu.forum.dao.CommunityDao;
import com.xiaoniu.forum.entity.CommunityEntity;
import com.xiaoniu.forum.entity.vo.CommunityTopicOptionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.Query;

import com.xiaoniu.forum.dao.CommunityCategoryDao;
import com.xiaoniu.forum.entity.CommunityCategoryEntity;
import com.xiaoniu.forum.service.CommunityCategoryService;


@Service("communityCategoryService")
public class CommunityCategoryServiceImpl extends ServiceImpl<CommunityCategoryDao, CommunityCategoryEntity> implements CommunityCategoryService {

    @Autowired
    private CommunityDao communityDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommunityCategoryEntity> page = this.page(
                new Query<CommunityCategoryEntity>().getPage(params),
                new QueryWrapper<CommunityCategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public String getCommunityCategoryNameById(String id) {
        CommunityCategoryEntity communityCategoryEntity = baseMapper.selectById(id);
        String name = communityCategoryEntity.getName();
        return name;
    }

    @Override
    public PageUtils searchByKw(String kw, Map<String, Object> params) {
        QueryWrapper<CommunityCategoryEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name",kw);
        IPage<CommunityCategoryEntity> page = this.page(
                new Query<CommunityCategoryEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    /**
     * 批量删除id 如果数据之间有关联关系 则删除失败
     * @param ids 需删除的id列表
     * @return 删除失败的id列表
     */
    @Override
    public Integer removeByIds(List<String> ids) {

        QueryWrapper<CommunityEntity> wrapper = new QueryWrapper<>();

        Integer failCount = 0;

        for (String id : ids) {
            wrapper = new QueryWrapper<>();
            wrapper.eq("category_id",id);
            Integer count = communityDao.selectCount(wrapper);
            System.out.println(count);
            if (count > 0){
                failCount ++;
            }else {
                this.baseMapper.deleteById(id);
            }
        }

        return failCount;
    }

    @Override
    public List<CommunityTopicOptionVo> getAllWithOptions() {
        List<CommunityTopicOptionVo> list = new ArrayList<>();

        List<CommunityCategoryEntity> communityCategoryEntityList = baseMapper.selectList(null);

        int index = 0;
        for (CommunityCategoryEntity communityCategoryEntity : communityCategoryEntityList) {
            CommunityTopicOptionVo communityTopicVo = new CommunityTopicOptionVo(communityCategoryEntity.getId(),communityCategoryEntity.getName(),index++);
            list.add(communityTopicVo);
        }
        return list;
    }


}